//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Common
{
    using System;
    using System.IO;
    using System.Text;
    using System.Xml.Serialization;

    /// <summary>
    /// Code for getting and setting the data product settings.
    /// </summary>
    public class DataProductGlobalSettingService
    {
        private static string tempFilePath = ".\\DataProductsSettings.XML";

        /// <summary>
        /// Gets the settings.
        /// </summary>
        /// <returns></returns>
        public static DataproductGlobalSetting GetSettings()
        {
            string configureXML = File.ReadAllText(tempFilePath);

            return DeSerializeSettings(configureXML);
        }

        /// <summary>
        /// Saves the settings.
        /// </summary>
        /// <param name="setting">The setting.</param>
        public static void SaveSettings(DataproductGlobalSetting setting)
        {
            string configureXML = SerializeSettings(setting);

            File.WriteAllText(tempFilePath, configureXML);
        }

        /// <summary>
        /// Serialize the Dataproduct Global settings.
        /// </summary>
        private static string SerializeSettings(DataproductGlobalSetting dataProductSettings)
        {
            StringBuilder configureXML = new StringBuilder();
            try
            {
                // Serialize to String builder.
                using (TextWriter writer = new StringWriter(configureXML))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(DataproductGlobalSetting));
                    serializer.Serialize(writer, dataProductSettings);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return configureXML.ToString();
        }

        /// <summary>
        /// Deserialize the Dataproduct Global settings.
        /// </summary>
        private static DataproductGlobalSetting DeSerializeSettings(string configureXML)
        {
            DataproductGlobalSetting supportedTypes = null;
            try
            {
                // Read the Configure xml.
                using (TextReader reader = new StringReader(configureXML))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(DataproductGlobalSetting));
                    object settingsObj = serializer.Deserialize(reader);
                    if (settingsObj != null)
                    {
                        supportedTypes = settingsObj as DataproductGlobalSetting;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }

            return supportedTypes;
        }
    }
}
